﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;
using System.Windows.Threading;

namespace SoundServant.Controls
{
    /// <summary>
    /// Interaction logic for RecordingControl.xaml
    /// </summary>
    public partial class RecordingReadOnlyControl : UserControl
    {
        StoredRecording recording;
        public delegate void VoidInvokeHandler();
        public RecordingReadOnlyControl(StoredRecording r)
        {
            InitializeComponent();

            recording = r;

            if (recording.Current)
            {
                recording.NewDiscCreated += new StoredRecording.NewDiscCreatedEventHandler(recording_NewDiscCreated);
                SS.TenthOfASecondTimer.Tick += new EventHandler(Timer_Tick);
            }

            DrawDiscs();
            Draw();

        }

        void Timer_Tick(object sender, EventArgs e)
        {
            DrawInfo();
        }

        void recording_NewDiscCreated()
        {
            this.Dispatcher.Invoke(DispatcherPriority.Normal, (VoidInvokeHandler)delegate()
            {
                DiscsHolder.Children.Add(new DiscReadOnlyControl(recording.CurrentDisc));
            });
        }

        void Draw()
        {
            this.Dispatcher.Invoke(DispatcherPriority.Normal, (VoidInvokeHandler)delegate()
            {
                RecordingDateTime.Content = recording.Date.ToString("ddd d MMMM yyyy HH:mm");
                if (recording.Title == "")
                    Title.Visibility = Visibility.Collapsed;
                else
                    Title.Content = recording.Title;
                Type.Content = SS.FullMeetingType(recording.Type);
                DrawInfo();
            });

        }

        void DrawInfo()
        {
            Info.Content = SS.ParseTimeSpan(recording.Length) + " | " + recording.Discs.Count + " Discs | " + recording.NumOfTracks + " Tracks | MP3 (" + Math.Round(((double)recording.Mp3File.Length / 1048576D), 2).ToString() + "Mb)";

        }
        void DrawDiscs()
        {
            foreach (StoredDisc disc in recording.Discs)
            {
                DiscsHolder.Children.Add(new DiscReadOnlyControl(disc));
            }
        }
    }
}
